package cn.org.seventh.tools.plugin;

import org.generama.FreeMarkerTemplateEngine;
import org.generama.JellyTemplateEngine;
import org.generama.QDoxCapableMetadataProvider;
import org.generama.WriterMapper;
import org.generama.defaults.JavaGeneratingPlugin;

import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;

public class ServiceMethodImplementsPlugin extends JavaGeneratingPlugin {
	/**
	 * 要替换的接口的名字
	 */
	private String interfacePackagereplace = "";
	/**
	 * 要替换的类名后缀
	 */
	private String suffix = "Manage";

	private String interfaceSuffix = "Service";

	/**
	 * 替换后的类名后缀
	 */
	private String suffixTo = "ServiceImpl";

	public String getSuffix() {
		return this.suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public String getSuffixTo() {
		return this.suffixTo;
	}

	public void setSuffixTo(String suffixTo) {
		this.suffixTo = suffixTo;
	}

	public String getInterfaceSuffix() {
		return this.interfaceSuffix;
	}

	public void setInterfaceSuffix(String interfaceSuffix) {
		this.interfaceSuffix = interfaceSuffix;
	}

	public String getInterfacePackagereplace() {
		return interfacePackagereplace;
	}

	public void setInterfacePackagereplace(String interfacePackagereplace) {
		this.interfacePackagereplace = interfacePackagereplace;
	}

	public ServiceMethodImplementsPlugin(JellyTemplateEngine templateEngine,
			QDoxCapableMetadataProvider metadataProvider,
			WriterMapper writerMapper) throws ClassNotFoundException {
		super(new FreeMarkerTemplateEngine(), metadataProvider, writerMapper);
		setMultioutput(true);
		setFileregex(suffix + "\\.java");
		setFilereplace(suffixTo + ".java");
		new TagLibrary(metadataProvider);
	}

	public ServiceMethodImplementsPlugin(
			FreeMarkerTemplateEngine templateEngine,
			QDoxCapableMetadataProvider metadataProvider,
			WriterMapper writerMapper) throws ClassNotFoundException {
		super(new FreeMarkerTemplateEngine(), metadataProvider, writerMapper);
		setMultioutput(true);
		setFileregex(suffix + "\\.java");
		setFilereplace(suffixTo + ".java");
		new TagLibrary(metadataProvider);
	}

	public boolean isAllowedMethod(Object metadata) {
		JavaMethod method = (JavaMethod) metadata;
		if (method.isNative() || method.isFinal() || method.isStatic()
				|| method.isPrivate()) {
			return false;
		}
		return true;
	}

	public boolean shouldGenerate(Object metadata) {
		JavaClass javaClass = (JavaClass) metadata;
		return javaClass.getTagByName(SpringManageTagImpl.NAME) == null ? false
				: true;
	}

	public String getInterfaceName(Object metadata) {
		String originalFilename = metadataProvider
				.getOriginalFileName(metadata);
		String result = originalFilename.replaceAll(suffix + "\\.java",
				interfaceSuffix);
		return result;
	}

	public String getVariableName(String name) {
		// JavaClass javaClass = (JavaClass) metadata;
		// String name = javaClass.getName();
		return name.substring(0, 1).toLowerCase()
				+ name.substring(1, name.length());
	}
}